/**
 * Created by jiachenpan on 16/11/18.
 */

export function isExternal (path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export function validUsername (str) {
  const validMap = ['admin', 'editor', 'lcnx','lcnx1','lcnx2','lcnx3','lcnx4','lcnx5', 'test', 'sightseer']
  return validMap.indexOf(str.trim()) >= 0
}

/* 合法uri */
export function validURL (url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/* 小写字母 */
export function validLowerCase (str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/* 大写字母 */
export function validUpperCase (str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/* 大小写字母 */
export function validAlphabets (str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validEmail (email) {
  const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(email)
}

const RegExpObj = {
  telephone: /^1\d{10}$/,
  phone: /^1\d{10}$/,
  email:/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/,
  customer_phone: /^1\d{10}$/,
  customer_email:/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/,
  id_number:/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
  passport_number:  /^1[45][0-9]{7}$|(^[P|p|S|s]\d{7}$)|(^[S|s|G|g|E|e]\d{8}$)|(^[Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8}$)|(^[H|h|M|m]\d{8,10}$)/
  // 护照样本 141234567, G12345678, P1234567
}

/**
 * 一开始计划等点击提交后，才校验所有需要校验的字段填写的内容格式是否正确
 * @param {*} source 
 * @param {*} fields 
 */
export function handleVerifyAll(source,fields){
  let _bool = true
  let _arr = []
  console.log(fields)
  fields.map(field=>{
    if(source[field]&&source[field].trim()){
      if(RegExpObj[field]&&RegExpObj[field].test(source[field])===false){
        _bool = false
        _arr.push(field)
      }
    }
  })
  return {_bool,_arr}
}

export function handleVerify(source,field){
  let _bool = true
  if(source[field]&&source[field].trim()){
    if(RegExpObj[field]&&RegExpObj[field].test(source[field])===false){
      _bool = false
    }else if(!RegExpObj[field]){
      _bool = false
      alert('没有找到匹配的校验规则')
    }
  }
  return _bool
}

export function handleGetTimestamp(time){
  return new Date(time).getTime()
}